<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/11/19
 * Time: 18:34
 */

namespace business\base\caches;

use common\cache\CacheManager;
use common\utils\DateTimeUtils;
use common\utils\RedisUtil;

class DataCache
{
    protected $redis;

    public function __construct()
    {
        $this->redis = \Yii::$app->redis;
    }

    /**
     * 存储记录到hash
     * @param string $prefix
     * @param string $name
     * @param array $data
     * @param int $expire
     * @return bool
     */
    public function saveRecordByHash
    (
        $prefix = '',
        $name = '',
        $data = [],
        $expire = 0
    ) {
        if (empty($name) || !is_array($data)) return false;

        $hashName = $prefix . ':' . $name;
        $params = RedisUtil::map2RedisParameter($data);
        array_unshift($params, $hashName);

        $this->redis->executeCommand('hmset', $params);
        !empty($expire) && $this->redis->expire($hashName, $expire);

        return true;
    }

    /**
     * 返回hash记录
     * @param string $prefix
     * @param string $name
     * @return array
     */
    public function getRecordByHash
    (
        $prefix = '',
        $name = ''
    ) {
        $hashName = $prefix . ':' . $name;
        return RedisUtil::RedisMapList2Map($this->redis->hgetall($hashName));
    }

    /**
     * 删除hash记录
     * @param string $prefix
     * @param string $name
     * @return mixed
     */
    public function delRecordByHash
    (
        $prefix = '',
        $name = ''
    ) {
        $hashName = $prefix . ':' . $name;
        return $this->redis->del($hashName);
    }

    /**
     * 获取上一次查询时间
     * @param string $cacheKey
     * @return array
     */
    public function getLastSearchTime($cacheKey = '', $incrementTime = 2)
    {
        if (!empty($cacheKey)) {
            // 获取上一次查询时间
            $searchTime = CacheManager::get($cacheKey);
            // 获取当前时间
            $currentSearchTime = DateTimeUtils::time();

            $lastSearchTime = '';

            if (empty($searchTime)) {
                // 默认查找3分钟内的变更项
//                $lastSearchTime = date('Y-m-d H:i:s',
//                    strtotime('-' . $incrementTime . ' minutes', $currentSearchTime));
                $lastSearchTime = date('Y-m-d H:i:s',
                    strtotime('-' . $incrementTime . ' days', $currentSearchTime));
            } else {
                list(, $lastSearchTime) = explode('==', $searchTime);
            }

            $timeSlot = [$lastSearchTime, date('Y-m-d H:i:s', $currentSearchTime)];

            CacheManager::set($cacheKey, join('==', $timeSlot));

            return $timeSlot;
        }
    }


    public function getLastQueryTime($cacheKey, $incrementTime = 2)
    {
        $now = DateTimeUtils::now();
        $lastQueryTime = CacheManager::get($cacheKey);
        if (empty($lastQueryTime)) {
            $lastQueryTime = date('Y-m-d H:i:s', strtotime('-' . $incrementTime . ' days' . $now));
        }
        CacheManager::set($cacheKey, $now);
        return [$lastQueryTime, $now];
    }

    /**
     * 判断值是否存在
     * @param string $value
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function existsValue($key = '', $value = 1)
    {
        return \Yii::$app->get('redis')->setnx($key, $value);
    }

    /**
     * 返回查找所有符合给定模式的KEY
     * @param string $key
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getKeys($key = '')
    {
        return \Yii::$app->get('redis')->keys($key);
    }

    /**
     * 获取指定 key 的值
     * @param string $key
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public function getValue($key = '')
    {
        return $this->redis->get($key);
    }
}
